package com.fcgzs.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fcgzs.springboot.mapper.CheckgroupCheckitemMapper;
import com.fcgzs.springboot.mapper.CheckgroupMapper;
import com.fcgzs.springboot.mapper.CheckitemMapper;
import com.fcgzs.springboot.pojo.Checkgroup;
import com.fcgzs.springboot.pojo.CheckgroupCheckitem;
import com.fcgzs.springboot.service.CheckgroupService;
import com.fcgzs.springboot.util.MessageConstant;
import com.fcgzs.springboot.util.PageResult;
import com.fcgzs.springboot.util.QueryPageBean;
import com.fcgzs.springboot.util.Result;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Service
public class CheckgroupServiceImpl implements CheckgroupService {

    @Resource
    private CheckgroupMapper checkgroupMapper;
    @Resource
    private CheckgroupCheckitemMapper checkgroupCheckitemMapper;

    //添加、更新检查组
    //添加：添加主表、添加中间表数据
    //更新：删除中间表相关数据、将新关系添加到中间表
    @Override
    public Result addCheckgroupInfo(Checkgroup checkgroup, Integer[] ids) {
        boolean flag = false;

        if(checkgroup.getId()!=null){ //更新
            flag =  checkgroupMapper.updateById(checkgroup)>0;
            //删除当前检查组id对应的中间表数据
            QueryWrapper<CheckgroupCheckitem> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("checkgroup_id",checkgroup.getId());
            //删除关系表中的数据
            flag = checkgroupCheckitemMapper.delete(queryWrapper) > 0;
        }else{ //添加
            flag = checkgroupMapper.insert(checkgroup)>0;
        }
        for(Integer itemid:ids){
            CheckgroupCheckitem checkgroupCheckitem = new CheckgroupCheckitem();
            checkgroupCheckitem.setCheckitemId(itemid);
            checkgroupCheckitem.setCheckgroupId(checkgroup.getId());
            flag = checkgroupCheckitemMapper.insert(checkgroupCheckitem) > 0;
        }
        if(flag){
            return new Result(flag, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }else{
            return new Result(flag, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }
    //分页查询
    @Override
    public PageResult queryCheckgroupPageInfo(QueryPageBean queryPageBean) {
        Page<Checkgroup> page = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        QueryWrapper<Checkgroup> queryWrapper = new QueryWrapper<>();
        if(queryPageBean.getQueryString()!=null&&queryPageBean.getQueryString().length()>0){
            //模糊查询的信息项
            queryWrapper.like("name",queryPageBean.getQueryString());
            queryWrapper.or();
            queryWrapper.like("code",queryPageBean.getQueryString());
            queryWrapper.or();
            queryWrapper.like("helpCode",queryPageBean.getQueryString());
        }
        Page<Checkgroup> checkgroupPage = checkgroupMapper.selectPage(page, queryWrapper);
        return new PageResult(checkgroupPage.getTotal(),checkgroupPage.getRecords());

    }
    //删除检查组
    @Override
    public Result deleteCheckgroupInfo(Integer id) {
        boolean flag = false;

        QueryWrapper<CheckgroupCheckitem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("checkgroup_id",id);
        //删除中间表中的数据
        flag = checkgroupCheckitemMapper.delete(queryWrapper) > 0;
        //删除主表数据
        flag = checkgroupMapper.deleteById(id) > 0;
        if(flag){
            return new Result(flag, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }else{
            return new Result(flag, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }
    //编辑回显数据
    @Override
    public Result getOneCheckgroupById(Integer id) {
        Checkgroup checkgroup = checkgroupMapper.selectById(id);
        //获取检查组对应的所有检查项id
        QueryWrapper<CheckgroupCheckitem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("checkgroup_id",id);
        List<CheckgroupCheckitem> checkgroupCheckitemList = checkgroupCheckitemMapper.selectList(queryWrapper);
        Integer [] ids = new Integer[checkgroupCheckitemList.size()];
        int i = 0;
        for(CheckgroupCheckitem one : checkgroupCheckitemList){
            ids[i] = one.getCheckitemId();
            i++;
        }
        //将检查组对象、检查组对应的检查项数组封装成Map集合
        Map<String, Object> map = new HashMap<>();
        map.put("onegroup",checkgroup);
        map.put("ids",ids);
        return new Result(true,"",map);
    }
    //展示所有检查组
    @Override
    public Result showAllGroupInfo() {
        return new Result(true,"",checkgroupMapper.selectList(null));
    }
}
